<template>
  <div class="app-container">
    <!--头部搜索框-->
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="人员类型" prop="userType">
        <el-select
          v-model="pagin.search.userType"
          placeholder="请选择人员类型"
          clearable
        >
          <el-option
            v-for="dict in typeList"
            :key="dict.value"
            :label="dict.value"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="职位" prop="postName">
        <el-input
          v-model="pagin.search.postName"
          placeholder="请输入职位"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 弹出框内容 -->
    <el-dialog
      v-if="dialogVisible"
      v-model="dialogVisible"
      :title="title"
      append-to-body
    >
      <el-form :model="userFrom" :rules="rules" label-width="120px">
        <!-- 表单项：人员编号 -->
        <el-form-item label="人员编号" prop="userId">
          <el-input
            v-model="userFrom.userId"
            placeholder="人员编号"
            readonly
          ></el-input>
        </el-form-item>
        <!-- 表单项：名称 -->
        <el-form-item v-model="userFrom.nickName" label="名称" prop="nickName">
          <el-input
            v-model="userFrom.nickName"
            placeholder="名称"
            readonly
          ></el-input>
        </el-form-item>
        <!-- 表单项：用户类型 -->
        <el-form-item label="用户类型" prop="userType">
          <el-select v-model="userFrom.userType" placeholder="请选择用户类型">
            <el-option label="经理" value="经理"></el-option>
            <el-option label="员工" value="员工"></el-option>
          </el-select>
        </el-form-item>

        <!-- <el-form-item label="职位" prop="postName">
          <el-select v-model="userFrom.postName" placeholder="请选择职位">
            <el-option label="总经理" value="1001"></el-option>
            <el-option label="采购部门经理" value="1002"></el-option>
            <el-option label="销售部门经理" value="1003"></el-option>
            <el-option label="仓储部门经理" value="1004"></el-option>
            <el-option label="财务部门经理" value="1005"></el-option>
            <el-option label="采购部门员工" value="1006"></el-option>
            <el-option label="销售部门员工" value="1007"></el-option>
            <el-option label="仓储部门员工" value="1008"></el-option>
            <el-option label="财务部门员工" value="1009"></el-option>
          </el-select>
        </el-form-item> -->

        <!-- 表单项：职位 -->
        <el-form-item label="职位" prop="postName">
          <el-select v-model="userFrom.postName" placeholder="请选择" clearable>
            <el-option
              v-for="dict in postList"
              :key="dict.postId"
              :label="dict.postName"
              :value="dict.postId"
            />
          </el-select>
        </el-form-item>

        <!-- 表单项：部门名称 -->
        <el-form-item label="部门名称" prop="deptName">
          <el-select v-model="userFrom.deptName" placeholder="请选择" clearable>
            <el-option
              v-for="dict in deptList"
              :key="dict.deptId"
              :label="dict.deptName"
              :value="dict.deptId"
            />
          </el-select>
        </el-form-item>
        <!-- 表单项：部门名称 -->
        <!-- <el-form-item label="部门名称" prop="deptName">
          <el-select v-model="userFrom.deptName" placeholder="请选择部门名称">
            <el-option label="销售部门" value="1001"></el-option>
            <el-option label="采购部门" value="1002"></el-option>
            <el-option label="仓储部门" value="1003"></el-option>
            <el-option label="财务部门" value="1004"></el-option>
          </el-select>
        </el-form-item> -->
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="submitForm()">确定</el-button>
          <el-button @click="resetForm">取消</el-button>
        </span>
      </template>
    </el-dialog>
    <!--数据展示-->
    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="checkList"
      row-key="userId"
      :default-expand-all="isExpandAll"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column
        prop="userId"
        label="人员编号"
        width="160"
      ></el-table-column>
      <el-table-column
        prop="nickName"
        label="名称"
        width="160"
      ></el-table-column>
      <el-table-column
        prop="userType"
        label="用户类型"
        width="160"
      ></el-table-column>
      <el-table-column
        prop="postName"
        label="职位"
        width="160"
      ></el-table-column>
      <el-table-column
        prop="deptName"
        label="部门名称"
        width="160"
      ></el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <!-- 修改按钮 -->
          <el-tooltip content="修改" placement="top">
            <el-button
              type="text"
              icon="Edit"
              @click="handleUpdate(scope.row)"
            ></el-button>
          </el-tooltip>
          <!-- 删除按钮 -->
          <el-tooltip content="删除" placement="top">
            <el-button
              type="text"
              icon="Delete"
              @click="handleDelete(scope.row)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <div class="pagination-container">
      <el-pagination
        class="pagiantion"
        @current-change="handleCurrentChange"
        :current-page="pagin.page"
        :page-size="pagin.count"
        layout="total, prev, pager, next, jumper"
        :total="pagin.total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script setup name="Dept">
//导入方法
import {
  deleteUser,
  listCheck,
  selectDept,
  selectPost,
  updata,
} from "@/api/reserve/user";
import { getCurrentInstance, reactive, ref } from "vue";

const { proxy } = getCurrentInstance();
// const { sys_normal_disable } = proxy.useDict("sys_normal_disable");

const checkList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const title = ref("");
const title2 = ref("");
const deptOptions = ref([]);
const isExpandAll = ref(true);
const refreshTable = ref(true);
const warehousingFormRef = ref(null); // 初始化为 null
const data = reactive({
  typeList: [
    {
      value: "经理",
    },
    {
      value: "员工",
    },
  ],
  postList: [],
  deptList: [
    // { value: "销售部门" },
    // { value: "采购部门" },
    // { value: "仓储部门" },
    // { value: "财务部门" },
  ],
  userFrom: {
    userId: undefined,
    nickName: undefined,
    userType: undefined,
    postName: undefined,
    deptId: undefined,
  },
  form: {},
  queryParams: {
    page: 1,
    total: 10,
    count: 5,
    search: {
      warehousingEntryId: undefined,
    },
  },
  pagin: {
    page: 1,
    total: 10,
    count: 5,
    search: {
      userType: undefined,
      postName: undefined,
      deptName: undefined,
    },
  },
});

const {
  queryParams,
  form,
  rules,
  pagin,
  warehousingForm,
  whdetailedList,
  deptList,
  typeList,
  userFrom,
  postList,
} = toRefs(data);

/** 查询人员部门列表 */
function getList() {
  if (pagin.value.page == null) {
    pagin.value.page = 1;
  }
  loading.value = true;
  listCheck(pagin.value).then((response) => {
    loading.value = false;
    checkList.value = response.list;
    pagin.value.page = response.current; // 将新的分页对象赋值给pagin引用
    pagin.value.total = response.total;
  });
}

//删除操作
function handleDelete(row) {
  proxy.$modal
    .confirm('是否确认删除编号为"' + row.userId + '"的数据项?')
    .then(function () {
      return deleteUser(row.userId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 新增修改提交按钮 */
const submitForm = async () => {
  updata(userFrom.value);
};

//修改按钮
async function handleUpdate(row) {
  //查询职位表
  {
    const response = await selectPost();
    postList.value = response;
  }
  //查询部门表
  {
    const response = await selectDept();
    deptList.value = response;
  }

  dialogVisible.value = true;
  userFrom.value.userId = row.userId;
  userFrom.value.nickName = row.nickName;
  userFrom.value.userType = row.userType;
  userFrom.value.postName = row.postName;
  // userFrom.value.deptId = row.deptId;
  userFrom.value.deptName = row.deptName;
  title.value = "修改";
}

//表单清空
function resetUserFrom() {
  userFrom.value = {
    userId: null,
    nickName: null,
    userType: null,
    postName: null,
    deptName: null,
  };
}
//切换页码
function handleCurrentChange(currentPage) {
  pagin.value.page = currentPage;
  //执行查询
  getList();
}
function handleChange(currentPage) {
  queryParams.value.page = currentPage;
  //执行查询
  getDetail();
}
//弹出框取消按钮
const resetForm = async () => {
  dialogVisible.value = false; // 关闭对话框
  resetUserFrom;
  // getList();
};
const dialogVisible = ref(false);
const dialogVisible2 = ref(false);
//新增按钮
function handleAdd() {
  dialogVisible.value = true; // 显示弹出框
  title.value = "新增"; //标题展示
}
function handleQuery() {
  getList();
}
getList();
</script>
<style></style>
